package xdu.ble.auth.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xdu.ble.common.utils.CookieUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author com.alibaba.lizhen
 * @Date 2021/5/28 4:08 下午
 * @Version 1.0
 * @Description
 */
@RestController
@RequestMapping("/auth")
public class Controller {

    @RequestMapping("/save")
    public String save(HttpSession session, @RequestParam("username") String username,
                       HttpServletRequest request,
                       HttpServletResponse response) {
        session.setAttribute("username", username);
        System.out.println("save method");
        //设置cookie和session
        CookieUtils.setCookie(request, response, "BLE_TOKEN", "183-4082-6754", 1800, true);
        return session.getId();
    }

    @RequestMapping("/get")
    public String get(HttpSession session, HttpServletRequest request) {
        System.out.println("get method");
        if (request.getCookies() == null) {
            System.out.println("cookies null!");
        } else {
            for (Cookie cookie : request.getCookies()) {
                if(cookie.getName().equals("BLE_TOKEN")){
                    System.out.println(cookie.getValue());
                }
            }
        }
        return session.getId() + "  -----  " + session.getAttribute("username");
    }

}
